//
//  LBWStatusLabel.swift
//  weibo
//
//  Created by Limonwei on 15/12/18.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class LBWStatusLabel: UILabel {
    
    var linkResults: [LBWMatchResult]?
    
    private lazy var linkView: [UIView] = [UIView]()
    
    override var attributedText: NSAttributedString? {
        didSet{
            
            textView.attributedText = attributedText
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //开启用户交互
        userInteractionEnabled = true
        //添加控件
        addSubview(textView)
        
        //添加约束
        textView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        }
    }
    
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let location = touches.first!.locationInView(self)
        
        //获取到手指点击位置的字符
        let textRange = textView.characterRangeAtPoint(location)
        textView.selectedTextRange = textRange

        //用户搜至点击的文字范围
        let range = textView.selectedRange
        
        if linkResults == nil {
            return
        }
        
        //遍历特殊字符 如:@xx http://xx #xx#
        for value in linkResults! {
            //判断手指点击字符是否在当前特殊字符范围内
            if NSLocationInRange(range.location, value.range) {
                textView.selectedRange = value.range
                
                let rects = textView.selectionRectsForRange(textView.selectedTextRange!)
                
                for value in rects {
                    let rect = value as! UITextSelectionRect
                    let v = UIView(frame: rect.rect)
                    //圆角
                    v.layer.cornerRadius = 3
                    v.layer.masksToBounds = true
                    v.backgroundColor = RGB(red: 177, green: 215, blue: 255)
                    
                    insertSubview(v, atIndex: 0)
                    linkView.append(v)
                }
                //此处可以做点击处理
            }
        }
    }
    
    //在手指离开的时候移除高亮
    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        for v in linkView {
            v.removeFromSuperview()
        }
        linkView.removeAll()
    }

    //手指结束点击
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touchesCancelled(touches, withEvent: event)
    }
    
    //懒加载用于监听手指点击位置的textView
    private lazy var textView: UITextView = {
    
        let textView = UITextView()
        
        //不允许编辑
        textView.editable = false
        //不允许交互
        textView.userInteractionEnabled = false
        //不允许滚动
        textView.scrollEnabled = false
        //设置透明度
        textView.alpha = 0
        //设置边距
        textView.textContainerInset = UIEdgeInsetsMake(0,-5,0,-5)
    
    
        return textView
    }()
    

}




















